package org.sample3;

import liquibase.Contexts;
import liquibase.LabelExpression;
import liquibase.Liquibase;
import liquibase.database.Database;
import liquibase.database.DatabaseFactory;
import liquibase.database.jvm.JdbcConnection;
import liquibase.resource.ClassLoaderResourceAccessor;
import liquibase.resource.DirectoryResourceAccessor;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;

public class Application {
    public static void main(String[] args) {
        Connection connection = null;
        try {
            // 1. 创建数据库连接
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "test", "test");

            // 2. 创建 Liquibase 实例
            Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection));
            Liquibase liquibase = new Liquibase("changelog-master.xml",
                    new DirectoryResourceAccessor(new File("E:\\data2\\MyCode\\liquibase-sample\\sample3-code\\script")), database);


            // 3. 执行数据库迁移
            liquibase.update(new Contexts(), new LabelExpression());
            liquibase.tag("v1.2");
            //liquibase.rollback(3, new Contexts(), new LabelExpression());

            System.out.println("Database migration completed successfully.");

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 4. 关闭数据库连接
            if (connection != null) {
                try {
                    connection.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
